package com.rgsx.service.serviceImpl;

import com.alibaba.fastjson.JSONObject;
import com.rgsx.dao.UserDao;
import com.rgsx.entiy.Position;
import com.rgsx.pojo.User;
import com.rgsx.redisEntiy.CodeRedisPojo;
import com.rgsx.redisEntiy.UserRedisPojo;
import com.rgsx.service.UserService;
import com.rgsx.utils.*;
import com.rgsx.vo.ResultVo;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public ResultVo login(File file) {
        try {
            //放入参数
            HashMap<String, String> map = new HashMap<>();
            map.put("name","");
            map.put("region","");

            JSONObject jsonObject = HttpUtil.doPostNoSSL(
                    "https://1.14.98.160:4000/api/file_rec_identify",
                    file,map);

            HashMap<String, Object> result = new HashMap<>();
            result.put("identify",jsonObject.get("identify"));
            result.put("name",jsonObject.get("name"));
            return ResultVo.success("成功",result);

        } catch (Exception e) {
            return ResultVo.fail(e);
        }
    }

    @Override
    public Map<String,Object> login2(String userName, String password) throws Exception {
        User user = userDao.login2(Md5Encrypt.md5(password), userName);
        HashMap<String, Object> result = new HashMap<>();
        if(user == null){
            return null;
        }

        result.put("token",JWTUtil.getToken(user.getId()));
        result.put("user",user);
        return result;
    }

    @Override
    public ResultVo register2(String uuid, String code ,String password,int identity) {
        CodeRedisPojo codeRedisPojo = (CodeRedisPojo) redisTemplate.opsForValue().get("uuid:" + uuid);

        if(codeRedisPojo == null){
            return ResultVo.fail("验证码过期","2");
        }
        if(!codeRedisPojo.getCode().equals(code)){
            return ResultVo.fail("验证码错误","3");
        }

        boolean isSuccess = userDao.register2(new User(null, null, null, identity, Md5Encrypt.md5(password), codeRedisPojo.getPhone()));
        if(!isSuccess){
            return ResultVo.fail("注册失败","4");
        }

        return ResultVo.success("注册成功",null);
    }

    @Override
    public ResultVo getUserInfo(String token) {
        long userId = JWTUtil.getUserId(token);
        UserRedisPojo userInfo = userDao.getUserInfo(userId);
        return ResultVo.success("获取成功",userInfo);
    }

    @Override
    public ResultVo setUserLocation(long userId, Position position) {
        return ResultVo.success("设置用户位置是没有必要的",null);
    }

    @Override
    public ResultVo sendVerificationCode(String phone) {
        String code = GetRandomCode.getCodeLength4();
        UUID uuid = UUID.randomUUID();
        boolean isSuccess = userDao.setRedisPhoneCode(code, phone,uuid.toString());
        if(!isSuccess)return ResultVo.fail("redis缓存写入失败","2");
        try {
            SendMessageUtil.sendMessage(code,"5",phone);
        } catch (TencentCloudSDKException e) {
            return ResultVo.fail(e);
        }

        return ResultVo.success("短信发送成功",uuid);
    }


}
